package com.atguigu.user.service.impl;

import com.atguigu.constants.UserConstants;
import com.atguigu.param.PageParam;
import com.atguigu.param.UserCheckParam;
import com.atguigu.param.UserLoginParam;
import com.atguigu.pojo.User;
import com.atguigu.user.mapper.UserMapper;
import com.atguigu.user.service.UserService;
import com.atguigu.utils.MD5Util;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public R check(UserCheckParam userCheckParam) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userCheckParam.getUserName());

        Long total = userMapper.selectCount(queryWrapper);
        if(total==0){
            return R.ok("账号不存在，可以注册！");
        }
        return R.fail("账号已存在，不可注册!");
    }

    @Override
    public R register(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",user.getUserName());

        Long total = userMapper.selectCount(queryWrapper);
        if(total>0){
            return R.fail("账号已存在，不可注册！");
        }

        // md5加密
        String newPwd = MD5Util.encode(user.getPassword() + UserConstants.USER_SALT);
        user.setPassword(newPwd);

        int rows = userMapper.insert(user);
        if(rows==0){
            return R.fail("注册失败！请稍后再试！");
        }
        return R.ok("注册成功！");
    }

    @Override
    public R login(UserLoginParam userLoginParam) {

        String newPwd = MD5Util.encode(userLoginParam.getPassword() + UserConstants.USER_SALT);

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userLoginParam.getUserName());
        queryWrapper.eq("password",newPwd);
        User user = userMapper.selectOne(queryWrapper);

        if(user==null){
            return R.fail("账号或者密码错误！");
        }

        // 不返回密码属性
        user.setPassword(null);
        return R.ok("登录成功",user);
    }

    @Override
    public R list(PageParam pageParam) {

        Page<User> page = new Page<>(pageParam.getCurrentPage(),pageParam.getPageSize());
        page = userMapper.selectPage(page, null);
        long total = page.getTotal();
        List<User> records = page.getRecords();
        return R.ok("查询成功",records,total);

    }

    @Override
    public R remove(Integer userId) {

        int i = userMapper.deleteById(userId);
        return R.ok("删除用户成功");
    }

    @Override
    public R update(User user) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        queryWrapper.eq("password",user.getPassword());
        Long count = userMapper.selectCount(queryWrapper);
        if(count==0){// 说明密码被修改
            user.setPassword(MD5Util.encode(user.getPassword()+UserConstants.USER_SALT));
        }

        userMapper.updateById(user);
        return R.ok("用户修改成功");
    }

    @Override
    public R save(User user) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",user.getUserName());

        Long total = userMapper.selectCount(queryWrapper);
        if(total>0){
            return R.fail("账号已存在，不可添加！");
        }

        // md5加密
        String newPwd = MD5Util.encode(user.getPassword() + UserConstants.USER_SALT);
        user.setPassword(newPwd);

        int rows = userMapper.insert(user);
        if(rows==0){
            return R.fail("添加失败！请稍后再试！");
        }
        return R.ok("添加成功！");
    }
}
